Ext.namespace('SLM');

SLM.DwcMatrix = function(config) {

	this.proxy ="";
	if (Config.mode == 'local') {
		this.proxy = new Ext.data.HttpProxy({
			url: Config.baseUrl + 'resources/api/app.php'
		});
	} else {
		this.proxy = new Ext.data.ScriptTagProxy({
			url: Config.baseUrl + 'resources/api/app.php'
		})
	}
	
	this.store = new Ext.data.JsonStore({
			proxy: this.proxy
		,	baseParams: { 
					cmd: 'checklist-region'
				,	values: Config.dwcMatrixFields
			}
		,	sortInfo: {field: 'event_type', direction: "ASC"}
		,	remoteSort: true		
		,	root: 'records'
		,	totalProperty: 'totalCount'
		,	fields: [
					'ScientificName'
				, 'R0'
				, 'R1'
				, 'R2'
				, 'R3'
				, 'R4'
				, 'R5'
				, 'R6'
				, 'R7'
				, 'R8'
				, 'R9'
				, 'R10'
				, 'R11'
				, 'R12'
				, 'R13'
				, 'R14'
				, 'R15'
				, 'R16'
				, 'R17'
				, 'R18'
				, 'R19'
				, 'R20'
				, 'R21'
				, 'R22'
				, 'R23'
				, 'R24'
				, 'R25'
				, 'R26'
				, 'R27'
				, 'R28'
				, 'R29'
				, 'R30'
			]
	});
	this.filters = new Ext.ux.grid.GridFilters({
		encode: true
	,	filters: [{
			type: 'string'
		,	dataIndex: 'ScientificName'
		}]
	});
  var pagingBar = new Ext.PagingToolbar({
			pageSize: 100
		,	store: this.store
		,	displayInfo: true
		,	displayMsg: 'Displaying Species {0} - {1} of {2}'
		,	emptyMsg: "No species to display"
	});

	Ext.apply(this, config, {
			id: 'dwc-matrix'
		,	store: this.store
		,	iconCls: 'icon_events_matrix'
		,	title: 'Species (Matrix View)'
		, loadMask: true
		,	plugins: [ this.filters ]
		,	columns: Config.dwcMatrixColumns
		,	border: false
		,	sm: new Ext.grid.RowSelectionModel({singleSelect: true})
		,	bbar: pagingBar
		,	listeners: {
				rowcontextmenu: this.rightClickMenu	
			}
	});

	SLM.DwcMatrix.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.DwcMatrix, Ext.grid.GridPanel, {
	rightClickMenu: function(grid, row, e){
		grid.getSelectionModel().selectRow(row);
		var record = grid.getSelectionModel().getSelected().data;
		var items = [{			
				text: 'GBIF Checklist Bank'
			,	iconCls:'icon_gbif'
			,	scope: this
			,	handler: function(){
					window.open('http://ecat-dev.gbif.org/search?q=' + record.ScientificName + '&rkey=1', '_blank');
				}
			},{
				text: 'ITIS: Catalog of Life'
			,	iconCls: 'icon_itis'
			,	handler: function(){
					window.open('http://www.catalogueoflife.org/annual-checklist/2010/search/all/key/' + record.ScientificName+ 'match/1', '_blank');
				}
			}, {
				text:'International Plant Names Index'
			,	handler: function() {
					window.open( 'http://www.ipni.org/ipni/advPlantNameSearch.do?find_infraspecies=&find_infragenus=&find_publicationTitle=&find_includePublicationAuthors=on&find_isIKRrecord=on&find_species=' + record.ScientificName + '&find_isGCIRecord=on&find_authorAbbrev=&find_includeBasionymAuthors=on&find_infrafamily=&find_genus=' + record.Genus + '&find_family=&find_isAPNIRecord=on', '_blank');
				}
			},{
				text: 'GBIF'
			,	iconCls: 'icon_gbif'
			,	scope: this
			,	handler: function(){
					window.open('http://data.gbif.org/search/' + record.ScientificName + '?', '_blank');
				}
			}, {
				text:'Flora of North America'
			,	iconCls: 'icon_fna'
			,	handler: function() {
					window.open( 'http://www.efloras.org/browse.aspx?name_str=' + record.ScientificName, '_blank');
				}
			}, {
				text:'NatureServe'
			,	iconCls: 'icon_natureserve'
			,	handler: function() {
					window.open( 'http://www.natureserve.org/explorer/servlet/NatureServe?searchName=' + record.ScientificName, '_blank');
				}
			}, {
				text:'USDA PLANTS'
			,	iconCls: 'icon_usda'
			,	handler: function() {
					window.open( 'http://plants.usda.gov/java/nameSearch?keywordquery=' + record.ScientificName + '&mode=sciname', '_blank');
				}
			}, {
				text:'Tropicos'
			,	iconCls: 'icon_fna'
			,	handler: function() {
					window.open( 'http://www.tropicos.org/NameSearch.aspx?name=' + record.ScientificName, '_blank');
				}
			}, {
				text:'Biodiversity Heritage Library'
			,	iconCls: 'icon_bhl'
			,	handler: function() {
					window.open( 'http://www.biodiversitylibrary.org/Search.aspx?searchTerm=' + record.ScientificName + '&searchCat=N', '_blank');
				}
			}, '-', {
				text:'Google Images'
			,	iconCls: 'icon_google'
			,	handler: function() {
					window.open( 'http://www.google.com/images?safe=yes&q=%22' + record.ScientificName + '%22', '_blank');
				}
			}, {
				text:'Flickr'
			,	iconCls: 'icon_flickr'
			,	handler: function() {
					window.open( 'http://www.flickr.com/search/?q=' + record.ScientificName, '_blank');
				}
			}, {
				text:'PicSearch'
			,	handler: function() {
					window.open( 'http://www.picsearch.com/search.cgi?q=' + record.ScientificName + '&cols=5&thumbs=20'	, '_blank');
				}
			}]
		var menu = new Ext.menu.Menu({
				items: items
		});
		var xy = e.getXY();
		menu.showAt(xy);
	}
});

Ext.reg('DwcMatrix', SLM.DwcMatrix);